package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import com.hyphenate.chat.EMClient
import com.hyphenate.chat.EMGroupManager
import com.hyphenate.chat.EMGroupOptions
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import fhp.hlhj.giantfold.utils.LogUtil
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.customview.ChooseJiePanDialog
import hlhj.fhp.supreme.customview.ChooseJiePanPop
import hlhj.fhp.supreme.javabean.*
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.toast
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_start_jie_pan_aty.*
import java.net.URL

class StartJiePanAty : BaseAty(), ChooseJiePanDialog.OnSelectListener, ChooseJiePanPop.OnChooseListener {

    override fun getContentId(): Int {
        return R.layout.activity_start_jie_pan_aty
    }
    private lateinit var chooseJiePanPop:ChooseJiePanPop
    private var cid=""
    private var isOk=false
    private val datas=ArrayList<JiePanCatBean.DataBean>()
    override fun initView() {

        /*OkGo.post<UserInfoBean>(Urls.BaseUrl+Urls.UserInfo)
                .execute(object :JsonCallBack<UserInfoBean>(this){
                    override fun onSuccess(response: Response<UserInfoBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            if (bean?.data.isroom.equals("0")){
                                isOk=false
                            }else{
                                isOk=true
                            }
                        }
                    }
                })*/
    }

    private var mWidthPixels: Int=0

    private var mHeightPixels: Int=0
    private var popHight=0
    override fun initListener() {
        btExit.setOnClickListener { finish() }
        btCommit.setOnClickListener {
            /*if (etTittle.text.isEmpty()||etDes.text.isEmpty()){
                MyUtils.toast("请完善信息")
            }else{
                if (!isOk)return@setOnClickListener
                Thread(Runnable {
                    val option = EMGroupOptions()
                    option.maxUsers = 200
                    option.style = EMGroupManager.EMGroupStyle.EMGroupStylePublicOpenJoin
                    option.inviteNeedConfirm=false
                    val strings = arrayOfNulls<String>(0)
                    val group = EMClient.getInstance().groupManager().createGroup(etTittle.text.toString(), etDes.text.toString(), strings, null, option)
                    LogUtil.logi("创建的群组是否为空${group}")
                    if (group!=null){
                        OkGo.post<BaseBean>(Urls.BaseUrl+Urls.CREATE_ROOM)
                                .params("id",group.groupId)
                                .execute(object :JsonCallBack<BaseBean>(this@StartJiePanAty){
                                    override fun onSuccess(response: Response<BaseBean>?) {
                                        val bean = response?.body()
                                        if (bean?.code==1){
                                            MyUtils.toast("解盘开始！")
                                            val intent = Intent(this@StartJiePanAty, JiePanDetailAty::class.java)
                                            intent.putExtra("roomId",group.groupId)
                                            startActivity(intent)
                                        }else{
                                            MyUtils.toast(bean?.msg)
                                        }
                                        finish()
                                    }
                                })
                    }
                    LogUtil.logi(group.isMemberOnly.toString())
                }).start()
            }*/
            if (etTittle.text.isEmpty()){
                this.toast(this@StartJiePanAty,"标题不能为空")
                return@setOnClickListener
            }
            if (etDes.text.isEmpty()){
                this.toast(this@StartJiePanAty,"简介不能为空")
                return@setOnClickListener
            }
            if (cid.isEmpty()){
                this.toast(this@StartJiePanAty,"类型不能为空")
                return@setOnClickListener
            }
            val intent = Intent(this, ArticleEditAty::class.java)
            intent.putExtra("cid",cid)
            intent.putExtra("tittle",etTittle.text.toString())
            intent.putExtra("des",etDes.text.toString())

            startActivity(intent)
            finish()
        }
        btType.setOnClickListener {
            if (datas.size==0){
                OkGo.post<JiePanCatBean>(Urls.BaseUrl+Urls.JIEPAN_CAT)
                        .execute(object :JsonCallBack<JiePanCatBean>(this@StartJiePanAty){
                            override fun onSuccess(response: Response<JiePanCatBean>?) {
                                val panCatBean = response?.body()
                                if (panCatBean?.code==1){
                                    datas.addAll(panCatBean?.data)
                                    chooseJiePanPop=ChooseJiePanPop(this@StartJiePanAty,datas,this@StartJiePanAty)
                                    chooseJiePanPop.showAsDropDown(drop)
                                }
                            }
                        })
            }else{
                if (chooseJiePanPop.isShowing){
                    chooseJiePanPop.dismiss()
                }else{
                    chooseJiePanPop.showAsDropDown(drop)
//                    chooseJiePanPop.showAtLocation(btType,Gravity.RIGHT,100,-popHight)
                }
            }

        }
    }

    override fun choose(name: String, id: String) {
        btType.text=name
        cid=id
    }

    override fun yes(str: String, id: String) {
        btType.text=str
        cid=id
    }
}
